#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest

from dyn_plot import *

def get_init_data(size):
    masses = [[50], [100], [150]]
    masses = masses[0:size]
    realSettings = RealSettings(yRealMax = 288, yRealMin = -72, xRealWidth = 400,
                                xRealStep = 50, yRealStep = 50)
    colors = [(0x55, 0x55, 0xFF), (0xFF, 0x55, 0xFF), (0x55, 0x55, 0x55)]
    names = [u"Blue Graph", u"Red Graph", u"Grey Graph"]
    curves = [Curve(mass, color, name) for (mass, color, name) in zip(masses, colors, names)]
    return realSettings, RealKoeffs(0.5, 1.0), curves


class TestDynPlot(unittest.TestCase):

    def testInit(self):
        realSettings, realKoeffs, curves = get_init_data(3)
        graph = DynPlot(realSettings, realKoeffs, curves)

        self.assertEqual(graph.get_curves(), curves) # hmm, maybe I must use copy in __init__
        self.assertEqual(len(graph.get_curves()), 3)

        self.assertEqual(graph.get_koeffs(), realKoeffs)
        self.assertEqual(graph.get_koeffs().kxRtoS, realKoeffs.kxRtoS)

        self.assertEqual(graph.get_settings(), realSettings)
        self.assertEqual(graph.get_settings().yRealMax, realSettings.yRealMax)


    def test_set_get_curves(self):
        realSettings, realKoeffs, curves = get_init_data(0)
        graph = DynPlot(realSettings, realKoeffs, curves)

        masses = [[50, 55, 52], [100, 101, 103], [150, 151, 154]]
        colors = [(0x55, 0x55, 0xFF), (0xFF, 0x55, 0xFF)]
        names = [u"Yellow Graph", u"Red Graph"]
        curves = [Curve(mass, color, name) for (mass, color, name) in zip(masses, colors, names)]

        graph.set_curves(curves)
        self.assertEqual(graph.get_curves(), curves)
        self.assertEqual(len(graph.get_curves()), 2)
        self.assertEqual(graph.get_curves()[1].mass[2], 103)
        self.assertEqual(graph.get_curves()[0].color, (0x55, 0x55, 0xFF))
        self.assertEqual(graph.get_curves()[0].name, u"Yellow Graph")

    def test_set_get_koeffs(self):
        realSettings, realKoeffs, curves = get_init_data(1)
        graph = DynPlot(realSettings, realKoeffs, curves)

        graph.set_koeffs(RealKoeffs(15.1, 23.7))
        self.assertEqual(graph.get_koeffs().kxRtoS, 15.1)
        self.assertEqual(graph.get_koeffs().kyRtoS, 23.7)

    def test_set_get_settings(self):
        realSettings, realKoeffs, curves = get_init_data(1)
        graph = DynPlot(realSettings, realKoeffs, curves)

        graph.set_settings(RealSettings(yRealMax = 441, yRealMin = -42, xRealWidth = 900,
                                xRealStep = 100, yRealStep = 50))
        self.assertEqual(graph.get_settings().yRealMax, 441)
        self.assertEqual(graph.get_settings().yRealMin, -42)
        self.assertEqual(graph.get_settings().xRealWidth, 900)
        self.assertEqual(graph.get_settings().xRealStep, 100)
        self.assertEqual(graph.get_settings().yRealStep, 50)

    def test_legends_visible(self):
        realSettings, realKoeffs, curves = get_init_data(2)
        graph = DynPlot(realSettings, realKoeffs, curves)

        graph.legends_visible(True)
        self.assertNotEqual(graph.drawer.additionOffsets[0], 0)

        graph.legends_visible(False)
        self.assertEqual(graph.drawer.additionOffsets[0], 0)



if __name__ == "__main__":

    load_case = unittest.defaultTestLoader.loadTestsFromTestCase
    suite = load_case(TestDynPlot)
    #suite.addTest(load_case(Test...))
    unittest.TextTestRunner(verbosity = 2).run(suite)

    #raw_input("press any key")
